package org.metasketch.editor.links;

import java.util.Arrays;
import java.util.List;

public class GraphicalLinkConfig implements ILinkConfig {

	GraphicalEndConfig source = new GraphicalEndConfig();

	GraphicalEndConfig target = new GraphicalEndConfig();

	String script;

	// String style;

	public GraphicalEndConfig getSource() {
		return source;
	}

	public GraphicalEndConfig isSourceOrTarget(String graphicType) {
		if (graphicType == null) {

			if (target.getTypes().size() == 0)
				return target;
			else if (target.getTypes().size() == 0)
				return target;
			else
				return null;
		}

		if (source.getTypes().contains(graphicType))
			return source;

		else if (target.getTypes().contains(graphicType))
			return target;

		return null;
	}

	public EndType getStrictFinishEndTypeFromGraphicType(
			String startGraphicType, String finishGraphicType) {

		if (startGraphicType != null && finishGraphicType != null) {

			if (source.getTypes().contains(startGraphicType)
					&& target.getTypes().contains(finishGraphicType))
				return EndType.TARGET;
			else
				return null;
		} else if (startGraphicType == null && finishGraphicType == null) {
			if (source.getTypes().size() == 0 && target.getTypes().size() == 0)
				return EndType.TARGET;
			else
				return null;
		} else if (startGraphicType == null && finishGraphicType != null) {

			if (source.getTypes().size() == 0
					&& target.getTypes().contains(finishGraphicType))
				return EndType.TARGET;
			else
				return null;
		} else if (startGraphicType != null && finishGraphicType == null) {

			if (source.getTypes().contains(startGraphicType)
					&& target.getTypes().size() == 0)
				return EndType.TARGET;
			else
				return null;
		}
		return null;

	}

	public EndType getFinishEndTypeFromGraphicType(String startGraphicType,
			String finishGraphicType) {

		if (startGraphicType == null && finishGraphicType == null) {
			if (source.getTypes().size() == 0 && target.getTypes().size() == 0)
				return EndType.TARGET;
			else
				return null;
		} else if (startGraphicType == null && finishGraphicType != null) {
			if (source.getTypes().size() == 0
					&& target.getTypes().contains(finishGraphicType))
				return EndType.TARGET;
			else if (target.getTypes().size() == 0
					&& source.getTypes().contains(finishGraphicType))
				return EndType.SOURCE;
			else
				return null;
		} else if (startGraphicType != null && finishGraphicType == null) {
			if (source.getTypes().size() == 0
					&& target.getTypes().contains(startGraphicType))
				return EndType.TARGET;
			else if (target.getTypes().size() == 0
					&& source.getTypes().contains(startGraphicType))
				return EndType.SOURCE;
			else
				return null;
		} else {

			if (source.getTypes().contains(startGraphicType)) {

				if (target.getTypes().contains(finishGraphicType))
					return EndType.TARGET;
				else
					return null;

			} else if (target.getTypes().contains(startGraphicType)) {

				if (source.getTypes().contains(finishGraphicType))
					return EndType.SOURCE;
				else
					return null;
			}
		}

		return null;

	}

	public void setSource(GraphicalEndConfig source) {
		this.source = source;
	}

	public GraphicalEndConfig getTarget() {
		return target;
	}

	public void setTarget(GraphicalEndConfig target) {
		this.target = target;
	}

	// source methods

	public void setSourceTypes(String... types) {
		source.setTypes(Arrays.asList(types));
	}

	public void setSourceTypes(List<String> types) {
		source.setTypes(types);
	}

	public void setSourceLinkEnd(LinkEnd linkEnd) {
		source.setLinkEnd(linkEnd);
	}

	public void setSourceFieldName(String fieldName) {
		source.setFieldName(fieldName);
	}

	public void setSourceSemanticFieldName(String fieldName) {
		source.setSemanticFieldName(fieldName);
	}

	// target methods

	public void setTargetTypes(String... types) {
		target.setTypes(Arrays.asList(types));
	}

	public void setTargetTypes(List<String> types) {
		target.setTypes(types);
	}

	public void setTargetLinkEnd(LinkEnd linkEnd) {
		target.setLinkEnd(linkEnd);
	}

	public void setTargetFieldName(String fieldName) {
		target.setFieldName(fieldName);
	}

	public void setTargetSemanticFieldName(String fieldName) {
		target.setSemanticFieldName(fieldName);
	}

	public String getScript() {
		return script;
	}

	public void setScript(String script) {
		this.script = script;
	}

	// public String getStyle() {
	// return style;
	// }
	//
	// public void setStyle(String style) {
	// this.style = style;
	// }

}
